package com.situ.day250122;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;

public class IODemo {
    @Test
    public void test() {
        try {//字符流--读内容
            FileReader fileReader = new FileReader("io.txt");
            int ch1 = fileReader.read();
            System.out.println((char) ch1);
            int ch2 = fileReader.read();
            System.out.println((char) ch2);
            int ch3 = fileReader.read();
            System.out.println((char) ch3);
        } catch (FileNotFoundException e) {
            //throw new RuntimeException(e);
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //用循环读
    @Test
    public void test2() {
        try {
            FileReader fileReader = new FileReader("io.txt");
            int ch = -1;//不定义变量下面报错，要boolon类型
            while ((ch = fileReader.read()) != -1) {//只要没读完就是true,如果这不定义ch，后面拿不到字符
                System.out.print((char) ch);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Test
    public void test3() {
        try {
            FileReader fileReader = new FileReader("a.txt");

            char[] buffer = new char[10];//buffer缓冲区
            int length = -1;
            /*length = fileReader.read(buffer);
            System.out.println(length);  10  10  6  -1
            length = fileReader.read(buffer);
            System.out.println(length);*/

            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(length);
                System.out.println(buffer);//第二次读不满十个 加上了上一次的
                System.out.println(Arrays.toString(buffer));

            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    //以上为读，以下为写
    @Test
    public void test4() {
        FileReader fileReader=null;
        FileWriter fileWriter=null;
        try {
            fileReader = new FileReader("a.txt");
            fileWriter = new FileWriter("a_back.txt");//复制写到这里面

            char[] buffer = new char[10];//buffer缓冲区
            int length = -1;


            while ((length = fileReader.read(buffer)) != -1) {
                System.out.println(length);
                System.out.println(buffer);//第二次读不满十个 加上了上一次的
                System.out.println(Arrays.toString(buffer));
                fileWriter.write(buffer, 0, length);

            }

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {//关闭流

            if (fileWriter != null) {
                try {
                    fileWriter.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fileReader != null) {
                try {
                    fileReader.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }

    }
}
